<?php

namespace App\Http\Controllers;

use App\Code;
use App\User;
use Illuminate\Http\Request;

class LoginController extends Controller
{
    //
    public function reg(Code $code)
    {
        $question = $code->getRand();
        return view("login.reg",compact("question"));
    }
    public function regStore(Request $request,Code $code)
    {
        $code->checkCode($request->account);
        $this->validate($request,[
            'phone'=>[
                "unique:users",
                'required',
                'regex:/^1[1-9]{1}[0-9]{9}$/'
            ],
            'account'=>'required',
        ]);
        
        $this->duanxin($request->phone,249358);
        return [
            "error"=>0,
            "info"=>'验证码已发送'
        ];

    }
    public function regCheck()
    {
        return view("login.check");
    }
    public function checkPass(Request $request)
    {
        if ($request->duanxin != session("duanxin")) {
            return [
                "error"=>1,
                "info"=>"验证短信错误请重新验证",
            ];
        }
        $this->validate($request,[
            'name'=>[
                "unique:users",
                'required',
                'max:20|min:2'
            ],
            'password'=>'required|confirmed|max:20|min:6',
        ]);
        $user = new User();
        $user->name = $request->name;
        $user->password = bcrypt($request->password);
        $user->phone = session("phone");
        $user->save();
        return[
            "error"=>0,
            "info"=>"注册成功",
        ];
    }
    public function login()
    {
        return view("login.login");
    }

    public function checkLogin(Request $request)
    {
        $this->validate($request,[
            'phone'=>[
                'required',
                'regex:/^1[1-9]{1}[0-9]{9}$/'
            ],
            'password'=>'required|max:20|min:6',
        ]);
        $user = request(["phone","password"]);
        $is_remember = boolval($request->remember);

        if (\Auth::attempt($user,$is_remember)) {
            return [
                "error"=>0,
                "info"=>"登陆成功",
                "id"=>\Auth::id(),
            ];
        }else{
            return [
                "error"=>1,
                "info"=>"账号或密码错误",
            ];
        }

    }
    public function phoneLogin(Request $request)
    {
        $this->validate($request,[
            'phone'=>[
                'required',
                'regex:/^1[1-9]{1}[0-9]{9}$/'
            ]
        ]);
        $res = \App\User::where("phone",$request->phone)->first();
        if (!$res){
            return[
            "error"=>1,
            "info"=>"该手机号码尚未注册",
        ];
        }
        $this->duanxin($request->phone,249358);
        return[
            "error"=>0,
        ];
    }

    public function duanxin($phone,$id)
    {
        //载入ucpass类
        require_once('../libs/Ucpaas.class.php');

        //初始化必填
        $options['accountsid']='ac7f80728012f2bf78379e75b00258e8';
        $options['token']='f1237e5ea39a46dc50b182e4050480a2';
        //初始化 $options必填
        $ucpass = new \Ucpaas($options);

        //开发者账号信息查询默认为json或xml
        header("Content-Type:text/html;charset=utf-8");

        $info = mt_rand(1000,9999);

        $appId = "983a2725dd6f4e15acd87056331946d7";
        $to = $phone;
        $templateId = $id;
        $param=$info;
        $ucpass->templateSMS($appId,$to,$templateId,$param);
        session(["duanxin"=>$param]);
        session(["phone"=>$phone]);
    }

    public function phoneCheck(Request $request)
    {
        $this->validate($request,[
            'phone'=>[
                'required',
                'regex:/^1[1-9]{1}[0-9]{9}$/'
            ],
            'duanxin'=>'required|size:4'
        ]);
        if (session("duanxin") !=$request->duanxin) {
            return[
                "error"=>1,
                "info"=>"短信验证码错误",
            ];
        }
        $res = \App\User::where("phone",$request->phone)->first();
        $user = request(["phone","duanxin"]);
        $is_remember = boolval($request->remember);
        if (\Auth::attempt($user,$is_remember)) {
            return [
                "error"=>0,
                "info"=>"登陆成功",
                "id"=>\Auth::id(),
            ];
        }else{
            return [
                "error"=>1,
                "info"=>"账号或密码错误",
            ];
        }
    }
    public function logout()
    {
        \Auth::logout();
    }

}
